package practice.dp;

public class Solution16 {
    public int findTargetSumWays(int[] nums, int target) {
        int sum = 0;
        for (int x : nums) {
            sum += x;
        }
        target = target < 0 ? -target : target;
        if (sum < target) return 0;
        if ((sum + target) % 2 == 1) return 0;
        target = (sum + target) >> 1;
        int[] dp = new int[target + 1];
        dp[0] = 1;
        for (int i = 0; i < nums.length; i++) {
            for (int j = target; j >= nums[i]; j--) {
                dp[j] += dp[j - nums[i]];
            }
        }
        return dp[target];
    }
}
